package com.gccloud.tgyfirst.controller.demo;

import com.gccloud.starter.common.constant.GlobalConst;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.controller.SuperController;
import com.gccloud.starter.core.dto.RepeatDTO;
import com.gccloud.starter.core.shiro.UserUtils;
import com.gccloud.starter.mybatis.page.PageVO;
import com.gccloud.starter.plugins.validator.ValidatorUtils;
import com.gccloud.tgyfirst.friend.dto.FriendDto;
import com.gccloud.tgyfirst.friend.entity.FriendEntity;
import com.gccloud.tgyfirst.friend.vo.FriendVo;
import com.gccloud.tgyfirst.service.friend.service.IFriendService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/friend")
@Api(tags = "朋友")
@ApiSort(value = 10)
public class FriendController extends SuperController {
    @Autowired
    private IFriendService friendService;
    @GetMapping("/")
    @ApiOperation(value = "列表", position = 10, notes = "分页查询列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<PageVO<FriendVo>> getPage(@ApiParam(name = "查询", value = "传入查询的业务条件", required = true) SearchDTO searchDTO) {
        PageVO<FriendEntity> page = friendService.getPage(searchDTO);
        PageVO PageVO = BeanConvertUtils.convert(page, PageVO.class);
        PageVO.setList(page.getList());
        return success(PageVO);  //VO：   响应数据给http客户端  （get方法）
    }

    /**
     * 根据id获取任务
     */

    @GetMapping("/{id}")
    @ApiOperation(value = "详情", position = 20, notes = "单个任务详情", produces = MediaType.APPLICATION_JSON_VALUE)

    public R<FriendVo> sel(@ApiParam(name = "任务ID", value = "传入String格式", required = true) @PathVariable("id") String id) {

        FriendEntity taskEntity = friendService.getById(id);

        FriendVo taskVO = BeanConvertUtils.convert(taskEntity, FriendVo.class);
        return R.success(taskVO);
    }

    /**
     * 新增任务
     *  需要新增一个工时entity
     */
    @PostMapping()
//    @RequiresPermissions("friend:add")
    @ApiOperation(value = "添加", notes = "添加任务", produces = MediaType.APPLICATION_JSON_VALUE)

    public R<String> ins(@ApiParam(name = "新增任务JSON对象", value = "传入Json格式", required = true) @RequestBody FriendDto friendDto) {
        //TaskDTO反射生成TaskEntity
        FriendEntity friendEntity = BeanConvertUtils.convert(friendDto, FriendEntity.class);
        System.out.println("拿到的ID："+friendEntity.getId());
        /**
         * 设置当前操作用户为创建人
         */
//        friendEntity.setFounder(UserUtils.getCurrentUserName());
        friendService.add(friendEntity);
        return success(friendEntity.getId());
    }

    @DeleteMapping("/{id}")
//    @RequiresPermissions("friend:delete")
    @ApiOperation(value = "删除", notes = "删除任务", produces = MediaType.APPLICATION_JSON_VALUE)

    public R<String> del(@ApiParam( name = "任务ID", value = "传入String格式", required = true) @PathVariable("id") String id) {

        friendService.del(id);
        return success();
    }


    /**
     *修改任务
     */
    @PutMapping
//    @RequiresPermissions("friend:update")
    @ApiOperation(value = "修改", notes = "修改任务", produces = MediaType.APPLICATION_JSON_VALUE)

    public R<String> upd(@ApiParam(name = "修改任务JSON对象", value = "传入json格式", required = true) @RequestBody FriendDto friendDto) {
        FriendEntity taskEntity = BeanConvertUtils.convert(friendDto,FriendEntity.class);
        friendService.upd(taskEntity);
        return R.success();
    }

    @PostMapping("/repeat")
    @ApiOperation(value = "查重", notes = "查重", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Boolean> repeat(@ApiParam(name = "查重", value = "传入JSON对象", required = true) @RequestBody RepeatDTO repeatDTO) {
        ValidatorUtils.validateEntity(repeatDTO);
        boolean repeat = friendService.repeat(repeatDTO.getId(), repeatDTO.getName(), repeatDTO.getValue(), GlobalConst.RepeatStrategy.CREATE_BY);
        return success(repeat);
    }
}
